package com.xj.demo.gcdemo;

import java.util.HashSet;
import java.util.Set;

public class ChangeHashCode {
    public static void main(String[] args) {
        Set<Point> set = new HashSet();
        Point point = new Point();
        point.setX(10);
        set.add(point);
        point.setX(20);
        System.out.println("set remove = " + set.remove(point));//false
        set.add(point);
        System.out.println("set size = " + set.size());//2
    }
}

class Point{
    private int x;
    public int getX() {
        return x;
    }
    public void setX(int x) {this.x = x;}
    //重写equals方法
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Point other = (Point) obj;
        return x == other.x;
    }
    //重写hashcode方法
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        return result;
    }

    @Override
    public String toString() {
        return "Point [x=" + x + "]";
    }
}
